﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Entities;
using TS.Commons;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class OwnerDataAccess
    {
        
            /// <summary>
            /// Parse to Entity
            /// </summary>
            /// <param name="dataRecord"></param>
            /// <returns></returns>
            public static Owner ParseToEntity(IDataRecord dataRecord)
            {
                return new Owner
                {
                    OwnerId = dataRecord["OwnerId"].ToStringOrDefault().ToNumeric(),
                    Name = dataRecord["Name"].ToStringOrDefault(),
                    Tel = dataRecord["Tel"].ToStringOrDefault(),
                    Email = dataRecord["Email"].ToStringOrDefault(),
                    Address = dataRecord["Address"].ToStringOrDefault()
                };
            }

            /// <summary>
            /// Get All Owner
            /// </summary>
            /// <returns></returns>
            public Collection<Owner> GetAllOwner()
            {
                Collection<SqlParameter> collection = new Collection<SqlParameter>
                {
                };
                int totalRecord = 0;
                using (IDataReader reader = SqlHelper.ExecuteReader(UIOwner.SelectAllOwnerQuery, collection))
                {
                    var result = new Collection<Owner>();
                    while (reader.Read())
                    {
                        Owner obj = ParseToEntity(reader);
                        result.Add(obj);
                    }
                    if (reader.NextResult())
                    {
                        if (reader.Read())
                        {
                            totalRecord = reader["TotalRowCount"].ToNumeric();
                        }
                    }
                    return result;
                }

            }

            /// <summary>
            /// Insert Owner
            /// </summary>
            /// <param name="monthWeek"></param>
            /// <returns></returns>
            public int InsertOwner(Owner owner)
            {
                int result = 0;
                if (owner != null)
                {
                    var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@Name",owner.Name),
                                                              new SqlParameter("@Tel",owner.Tel),
                                                              new SqlParameter("@Email",owner.Email),
                                                              new SqlParameter("@Address",owner.Address),
                                                          };

                    using (IDataReader reader = SqlHelper.ExecuteReader(UIOwner.InsertOwnerQuery, parameters))
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                result = Convert.ToInt32(reader["OwnerId"], CultureInfo.CurrentCulture);
                            }
                            return result;
                        }
                    }
                }

                return result;
            }

            /// <summary>
            /// Read Gateway Pagging
            /// </summary>
            /// <param name="pageSize"></param>
            /// <param name="pageIndex"></param>
            /// <param name="strFilter"></param>
            /// <param name="strOrderBy"></param>
            /// <param name="totalRecord"></param>
            /// <returns></returns>
            public Collection<Owner> ReadOwnerPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
            {
                var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@PageSize",pageSize),
                                                          new SqlParameter("@PageIndex",pageIndex),
                                                          new SqlParameter("@StringFilter",strFilter),
                                                          new SqlParameter("@OrderBy",strOrderBy)
                                                      };
                totalRecord = 0;

                //get result
                using (IDataReader reader = SqlHelper.ExecuteReader(UIOwner.OwnerSelectPaged, parameters))
                {
                    var result = new Collection<Owner>();
                    while (reader.Read())
                    {
                        Owner owner = ParseToEntity(reader);
                        owner.ShortAddress = Utility.WriteString(owner.Address, 20);
                        result.Add(owner);
                    }
                    if (reader.NextResult())
                    {
                        if (reader.Read())
                        {
                            totalRecord = reader["TotalRowCount"].ToNumeric();
                        }
                    }
                    return result;
                }
            }

        /// <summary>
        /// GetByOwnerId
        /// </summary>
        /// <param name="ownerId"></param>
        /// <returns></returns>
            public Owner GetByOwnerId(int ownerId)
            {
                var collection = new Collection<SqlParameter>
                                 {
                                     new SqlParameter
                                         (
                                         "@OwnerId",
                                         ownerId
                                         )
                                 };

                using (IDataReader iDataReader = SqlHelper.ExecuteReader(UIOwner.OwnerSelectById, collection))
                {
                    if (iDataReader.Read())
                    {
                        return ParseToEntity(iDataReader);
                    }
                }
                return null;
            }

            /// <summary>
            /// update Owner
            /// </summary>
            /// <param name="owner"></param>
            /// <returns></returns>
            public int UpdateOwner(Owner owner)
            {
                int result = 0;
                if (owner != null)
                {
                    var parameters = new Collection<SqlParameter>
                                     {
                                         new SqlParameter("@OwnerId", owner.OwnerId),
                                         new SqlParameter("@Email", owner.Email),
                                         new SqlParameter("@Name", owner.Name),
                                         new SqlParameter("@Tel", owner.Tel),
                                         new SqlParameter("@Address", owner.Address)
                                     };

                    using (IDataReader reader = SqlHelper.ExecuteReader(UIOwner.UpdateOwnerQuery, parameters))
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                result = Convert.ToInt32(reader["OwnerId"], CultureInfo.CurrentCulture);
                            }
                            return result;
                        }
                    }

                }
                return result;
            }

            /// <summary>
            /// DeleteOwner
            /// </summary>
            /// <param name="owner"></param>
            /// <returns></returns>
            public int DeleteOwner(Owner owner)
            {
                int result = 0;
                if (owner != null)
                {
                    var parameters = new Collection<SqlParameter>
                                     {
                                         new SqlParameter("@OwnerId", owner.OwnerId)
                                     };

                    using (IDataReader reader = SqlHelper.ExecuteReader(UIOwner.DelteOwnerQuery, parameters))
                    {
                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                result = Convert.ToInt32(reader["OwnerId"], CultureInfo.CurrentCulture);
                            }
                            return result;
                        }
                    }
                }

                return result;
            }
        }
    }
